package com.irdstudio.apicenter.console.controller.template;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.util.FootPage;
import com.irdstudio.apicenter.console.dm.util.FootPageDao;
import com.irdstudio.apicenter.console.service.template.SPageParamTradeInoutService;
import com.irdstudio.apicenter.console.service.template.SPageParamTradeService;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamTrade;
import com.irdstudio.apicenter.frame.maker.dm.SPageParamTradeInout;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/sPageParamTrade")
public class SPageParamTradeAction extends MacmsBaseAction {
	@Autowired
	private SPageParamTradeService sPageParamTradeService;
	@Autowired
	private SPageParamTradeInoutService sPageParamTradeInoutService;

	@RequestMapping(value = "jumpListPage")
	public String querySPageParamTradeList(HttpServletRequest request)
			throws Exception {
		String pageid = request.getParameter("pageId");
		if (StringUtils.isEmpty(pageid)) {
			throw new Exception("必须传入页面标识(pageid)参数!");
		}
		/*
		 * List<SPageParamTrade>
		 * list=sPageParamTradeService.queryByPageId(pageid);
		 * request.setAttribute("sPageParamTradeList", list);
		 */
		String qtcode = "";
		String qtname = "";
		if (request.getParameter("qtcode") != null) {
			qtcode = request.getParameter("qtcode");
		}
		if (request.getParameter("qtname") != null) {
			qtname = request.getParameter("qtname");
		}

		int row = 0;
		int size = 0;
		row = (null == request.getParameter("pageNo") ? 0 : Integer
				.parseInt(request.getParameter("pageNo")));
		size = (null == request.getParameter("pageSize") ? 0 : Integer
				.parseInt(request.getParameter("pageSize")));

		FootPageDao fpd = new FootPageDao();
		FootPage fp = new FootPage();

		fp = fpd.querypage("  s_page_param_trade a  where    a.PAGE_ID= '"
				+ pageid + "'", row, size);
		row = (fp.getPageNo() - 1) * fp.getPageSize();
		// 查询数据并放入到上下文中
		String sql = "SELECT * FROM s_page_param_trade WHERE PAGE_ID=? and  trade_code  like  '%"
				+ qtcode + "%'  and  trade_name like '%" + qtname + "%'";
		super.context.addDataList("atpList", this.getTableModelDAO().queryList(
				sql, new PageInfo(), new Object[] { pageid }));
		request.setAttribute("pageId", pageid);
		request.setAttribute("context", super.context);
		request.setAttribute("page", fp);
		return "admin/sPageParamTradeList";

	}

	@RequestMapping(value = "jumpEditPage")
	public String jumpEditPage(HttpServletRequest request) throws SQLException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		String tradeid = request.getParameter("tradeid");
		SPageParamTrade sPageParamTrade = sPageParamTradeService
				.queryWithKeys(tradeid);
		List<SPageParamTradeInout> listIn = sPageParamTradeInoutService
				.querySPageParamTradeInout(tradeid, "1");
		List<SPageParamTradeInout> listOut = sPageParamTradeInoutService
				.querySPageParamTradeInout(tradeid, "2");
		String appId = request.getParameter("appid");
		request.setAttribute("sPageParamTrade", sPageParamTrade);
		request.setAttribute("listIn", listIn);
		request.setAttribute("listOut", listOut);
		request.setAttribute("tradeid", tradeid);
		return "admin/editSpageParamTrade";
	}

	@RequestMapping(value = "jumpCreatePage")
	public String jumpCreatePage(HttpServletRequest request)
			throws SQLException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		String pageId = request.getParameter("pageId");
		request.setAttribute("pageId", pageId);
		return "admin/createSpageParamTrade";
	}

	@RequestMapping(value = "saveSpageParamTrade")
	public void saveSpageParamTrade(
			@ModelAttribute("sPageParamTrade") SPageParamTrade sPageParamTrade,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		int flag = -1;
		flag = sPageParamTradeService.insert(sPageParamTrade);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "deleteSpageParamTrade")
	public void deleteSpageParamTrade(
			@ModelAttribute("sPageParamTrade") SPageParamTrade sPageParamTrade,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		int flag = -1;
		String tradeid = request.getParameter("tradeid");
		flag = sPageParamTradeService.delete(tradeid);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "updateSpageParamTrade")
	public void updateSpageParamTrade(
			@ModelAttribute("sPageParamTrade") SPageParamTrade sPageParamTrade,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		// String appId = super.context.getRequestParams().getValue("appid");
		// request.setAttribute("appid", appId);
		// request.setAttribute("context", super.context);
		int flag = -1;
		flag = sPageParamTradeService.update(sPageParamTrade);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
